
/* Objective:  use municipality-year variation in 
the generosity in terms of allowances, conditional on children's 
diagnosis codes -- Inspired by Fadlon and Nilsen 2021-- */


/* 1st STEP: Take sample of all children that had a health shock during time period*/
* recover municipality info and icd diagnosis
clear
use "$processed_data/location"
merge 1:1 shnro_mother child_b_year using "$processed_data\date_diagnosis.dta"
drop _merge 
keep shnro_mother municipality_mother ICD9 ICD10 child_b_year
save  "$processed_data\additionaldata_IV", replace 


/* 2nd STEP: Merge allowances data with this additional data */
{
** IMPORT DATA AND MERGE 
use "$processed_data\date_diagnosis.dta", clear

** merge mothers income
merge 1:1 shnro_mother using "$processed_data\mothers_income_diagnosis_all", keep(3) nogen
merge 1:1 shnro_mother using "$processed_data\fathers_income_diagnosis_all", keep(3) nogen

*merge with location and diag data
merge 1:1 shnro_mother child_b_year using "$processed_data\additionaldata_IV", keep(3) nogen 

keep yearinp shnro_mother shnro_father municipality_mother ICD9 ICD10 ///
income_* father_income_* age_firstinphospital fam_allow_* transfers_* f_fam_allow_* father_transfers_*

reshape long income_ father_income_ fam_allow_ f_fam_allow_ transfers_ father_transfers_ ,i(shnro_mother) j(year_event) string
rename income_ income
rename father_income_ father_income
rename fam_allow_ fam_allow
rename transfers_ transfers
rename f_fam_allow_ f_fam_allow
rename father_transfers_ father_transfers


** gen numeric variable for time w.r.t health shock (health shock year==0)
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

drop if year_event == "merge" | year_event == "merge_dad"


** gen year of income (calendar year)
gen year_calendar=yearinp+time_event
replace fam_allow = 0 if fam_allow < 0

** Restrict to ages of our sample in the did:
keep if inrange(age_firstinphospital,7,18) & inrange(yearinp,1995,2014) 

/* 3rd STEP: create leave out measure for municipality-year variation */
* clean icdcodes variables, use only letter + 3 digits
g icd_new = substr(ICD10,1,4)
replace icd_new = substr(ICD9,1,4) if icd_new ==""

* encode municipality
encode icd_new, gen(icd_code)
encode municipality_mother, g(muni)

*clean variables*
* g transfers var
foreach var in  fam_allow {
bysort shnro_mother: egen `var'_post = sum (`var') if time_event >= 0 
}
keep if time_event == 1


* regress on ICD code fixed effects and generate residuals 
foreach var in  fam_allow_post {
reg `var' i.icd_code 
predict resid_`var', resid
}


* leave-out measure with adjusted mean 

**** Adjusted
* own observation
foreach var in fam_allow_post {
bysort shnro_mother: egen i_obs_`var' = count(resid_`var')
bysort shnro_mother: egen i_resid_`var' = mean(resid_`var')

* create muni-year variable
bys muni yearinp: egen temp_mean_`var' = mean(resid_`var') 
bys muni yearinp: egen temp_obs_`var' = count(resid_`var')

* leave out
gen muniy_iv_`var'= (temp_mean_`var'*temp_obs_`var' - (i_obs_`var'*i_resid_`var')) / (temp_obs_`var' - i_obs_`var') 
replace muniy_iv_`var' =. if temp_obs_`var'== 1 /* if only one obs in that muni-year */
}


keep shnro_mother muniy_iv* muni icd_code fam_allow_post




* drop outliers
keep if muniy_iv_fam_allow_post < 50000
save "$processed_data\variation_IVmuni", replace
}


**********************************************************
* Figure A8:  Distribution IV 
**********************************************************
{
use "$processed_data\variation_IVmuni", clear
histogram muniy_iv_fam_allow_post , xtitle("Distribution of (residualized) average family allowances") frac xlabel(-50000 -25000 0 25000 50000)
graph export "$output\figure A8.pdf",  as(pdf) replace
}

**********************************************************
* Table A13: Table IV 
**********************************************************
{
use "$processed_data\date_diagnosis.dta", clear 
merge 1:m shnro_mother using ///
"$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 shnro_mother treat using ///
"$processed_data\fathers_income_diagnosis4", keep(3) nogen 

merge m:1 shnro_mother using "$processed_data\variation_IVmuni", keep(3) nogen

keep yearinp shnro_mother shnro_father age_mother_inp age_father_inp ///
child_b_year male treat ageinptreat year_treat educ0 father_educ0 ///
transfers_* father_transfers* totalincome_* f_totalincome_* ///
income_* father_income_* fam_allow_* f_fam_allow_* muni* icd_code fam_allow_post muniy_iv*

* drop municipalities with too few observations 
bysort muni: egen temp_obs = count(shnro_mother)
drop if temp_obs < 200


rename fam_allow_post postfam_allow

egen new_shnromother = group(shnro_mother treat)
reshape long transfers_ father_transfers_ totalincome_ ///
f_totalincome_ income_ father_income_ fam_allow_ f_fam_allow_, ///
i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income
rename transfers_ transfers
rename father_transfers_ father_transfers
rename totalincome_ totalincome
rename f_totalincome_ f_totalincome
rename fam_allow_ fam_allow
rename f_fam_allow_ f_fam_allow
 
* gen numeric variable for time w.r.t hospitalization 
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_5 (-1 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Table A13
** Set controls
global controls i.treat i.age_mother i.age_father i.year_calendar ///
i.child_b_year i.educ0 i.father_educ0 male

** Implement conditions

g post =  1 if time_event >= 0 
replace post = 0 if post ==.
g post_treat = post*treat

keep if  yearinp < 2015



*** Regressions building on work by Fadlon and Nielsen code & paper

replace postfam_allow= 0 if post == 0 & postfam_allow !=.
replace postfam_allow = 0 if treat == 0 

g post_postfam_allow = post*postfam_allow
g treat_postfam_allow = treat*postfam_allow
g treat_post_postfam_allow= treat*post*postfam_allow

g treat_post_IV = treat*post*muniy_iv_fam_allow_post
g treat_IV = treat*muniy_iv_fam_allow_post




estimates clear
* reduced form 
reg income post treat post_treat treat_post_IV i.muni i.year_calendar i.child_b_year, cluster(shnro_mother)
estimates store reduced_form
sum income if time_event == -2
estadd scalar ymean = r(mean)
estadd local controls "Yes"


* IV estimation 
ivreg2 income post treat post_treat i.muni i.year_calendar i.child_b_year ( treat_post_postfam_allow = treat_post_IV ) , cluster(shnro_mother) savefirst savefprefix(first_)
estimates store IV
	mat first = e(first)
	estadd scalar firststage = round(first[4,1], .01)
sum income if time_event == -2
estadd scalar ymean = r(mean)
estadd local controls = "Yes"

label var post_treat 				"Post_t * Treat_i"
label var treat_post_postfam_allow 	"Post_t * Treat_i * SI_i "
label var treat_post_IV				"Post_t * Treat_i * Z_{msi}"

** Output 
esttab reduced_form IV ///
using "$output\Table_A13.tex", label replace ///
addnotes("This table shows in column (1) the reudced form, and column (2) the IV estimates for the interaction of the impact of a child's hospitalization with the genorisity of the social insurance system in terms of family allowances. The instrument is a leave-out residualized measure based on all children's health shocks that occurred in a municipality on a given year. Clustered standard errors at the parent level.") ///
mtitles("Reduced Form" "IV") ///
keep(post_treat treat_post_postfam_allow treat_post_IV  )  ///
order (post_treat treat_post_postfam_allow treat_post_IV ) ///
scalars ("controls Controls" "ymean Mean \$Y_{t-2}$" "firststage First Stage") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)
}

